package com.ruoyi.project.wms.essentials.purchase.service.impl;

import java.util.List;

import com.ruoyi.project.wms.essentials.details.domain.WmsPurchaseDetails;
import com.ruoyi.project.wms.essentials.details.mapper.WmsPurchaseDetailsMapper;
import com.ruoyi.project.wms.essentials.purchase.domain.WmsPurchase;
import com.ruoyi.project.wms.essentials.purchase.mapper.WmsPurchaseMapper;
import com.ruoyi.project.wms.essentials.purchase.service.IWmsPurchaseService;
import com.ruoyi.project.wms.product.domain.WmsProduct;
import com.ruoyi.project.wms.product.mapper.WmsProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.ruoyi.common.utils.text.Convert;

/**
 * 进货订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-29
 */
@Service
public class WmsPurchaseServiceImpl implements IWmsPurchaseService
{
    @Autowired
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private WmsPurchaseDetailsMapper wmsPurchaseDetailsMapper;

    @Autowired
    private WmsProductMapper wmsProductMapper;

    /**
     * 查询进货订单
     * 
     * @param purId 进货订单ID
     * @return 进货订单
     */
    @Override
    public WmsPurchase selectWmsPurchaseById(Integer purId)
    {
        return wmsPurchaseMapper.selectWmsPurchaseById(purId);
    }

    /**
     * 查询进货订单列表
     * 
     * @param wmsPurchase 进货订单
     * @return 进货订单
     */
    @Override
    public List<WmsPurchase> selectWmsPurchaseList(WmsPurchase wmsPurchase)
    {
        return wmsPurchaseMapper.selectWmsPurchaseList(wmsPurchase);
    }

    /**
     * 新增进货订单
     * 
     * @param wmsPurchase 进货订单
     * @return 结果
     */
    @Override
    public int insertWmsPurchase(WmsPurchase wmsPurchase)
    {
        return wmsPurchaseMapper.insertWmsPurchase(wmsPurchase);
    }

    /**
     * 修改进货订单
     * 
     * @param wmsPurchase 进货订单
     * @return 结果
     */
    @Override
    public int updateWmsPurchase(WmsPurchase wmsPurchase)
    {
        return wmsPurchaseMapper.updateWmsPurchase(wmsPurchase);
    }

    /**
     * 删除进货订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmsPurchaseByIds(String ids)
    {
        return wmsPurchaseMapper.deleteWmsPurchaseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除进货订单信息
     * 
     * @param purId 进货订单ID
     * @return 结果
     */
    @Override
    public int deleteWmsPurchaseById(Integer purId)
    {
        return wmsPurchaseMapper.deleteWmsPurchaseById(purId);
    }


    /**
     *
     * @param pId 商品id
     * @param purId 订货单id
     * @param detPurPCount  订货数量
     * @return
     */
    @Override
    public String updatePurOrder(Integer pId, Integer purId, Integer detPurPCount) {
        //查询商品信息
        WmsProduct wmsProduct=wmsProductMapper.selectWmsProductById(pId);
        //查询状态为未提交的订单
        WmsPurchase wmsPurStatus=new WmsPurchase();
        wmsPurStatus.setPurStatus("未提交");
        Integer nowCount=0;
        List<WmsPurchase> wmsPurchaseList=wmsPurchaseMapper.selectWmsPurchaseList(wmsPurStatus);
        for (WmsPurchase wp : wmsPurchaseList) {
            //查询所有未提交的订货单的商品详情
            WmsPurchaseDetails wmsPurchaseNo=new WmsPurchaseDetails();
            wmsPurchaseNo.setDetPurId(wp.getPurId());
            List<WmsPurchaseDetails> wmsPurchaseDetailsList = wmsPurchaseDetailsMapper.selectWmsPurchaseDetailsList(wmsPurchaseNo);
            for (WmsPurchaseDetails wd : wmsPurchaseDetailsList) {
                if (wd.getDetPurPId()== pId){
                    nowCount +=wd.getDetPurPCount();
                }
            }
        }
        //查看本订单内是否已经有该商品，如果有，修改订单详情内的商品数量
        WmsPurchaseDetails wmsPurchaseOld =new WmsPurchaseDetails();
        wmsPurchaseOld.setDetPurId(purId);
        List<WmsPurchaseDetails> wmsPurchaseDetailsList2 = wmsPurchaseDetailsMapper.selectWmsPurchaseDetailsList(wmsPurchaseOld);
        Boolean nohave =true;
        for (WmsPurchaseDetails wd2 : wmsPurchaseDetailsList2) {
            if (wd2.getDetPurPId()==pId){
                wd2.setDetPurPCount(wd2.getDetPurPCount()+detPurPCount);
                wmsPurchaseDetailsMapper.updateWmsPurchaseDetails(wd2);
                nohave =false;
            }
        }
        //如果没有，则添加详情
        if (nohave){
            WmsPurchaseDetails wmsPurchaseNew=new WmsPurchaseDetails();
            wmsPurchaseNew.setDetPurId(purId);
            wmsPurchaseNew.setDetPurPId(pId);
            wmsPurchaseNew.setDetPurPCount(detPurPCount);
            wmsPurchaseDetailsMapper.insertWmsPurchaseDetails(wmsPurchaseNew);
        }
        //获取当前订单商品总金额，计算添加后的总金额
        WmsPurchase wmsPurchase =wmsPurchaseMapper.selectWmsPurchaseById(purId);
        wmsPurchase.setPurMoney(wmsPurchase.getPurMoney()+(wmsProduct.getpCost()*detPurPCount));
        wmsPurchaseMapper.updateWmsPurchase(wmsPurchase);
        return "ok";
    }

    /**
     * 根据id修改状态
     * @param id
     * @return
     */
    @Override
    public int updateDisposeSub(Integer id) {
        WmsPurchase wmsPurchase=wmsPurchaseMapper.selectWmsPurchaseById(id);
        if (wmsPurchase.getPurMoney() <= 0){
            return -1;
        }
        wmsPurchase.setPurId(id);
        wmsPurchase.setPurStatus("已提交");
        return wmsPurchaseMapper.updateWmsPurchase(wmsPurchase);
    }
}
